import { ref } from 'vue'
import { useRouter } from 'vue-router'
import treeToFlat from '@/utils/treeToFlat'
export const useInputSearch = (data) => {
  const router = useRouter()
  let _data = data.filter((route) => route.meta && !route.meta.hidden)
  _data = treeToFlat(_data, [], 'children')
  _data = _data.filter((route) => !route.children)

  let listData = []
  _data.forEach((v) => {
    listData.push({
      id: v.path,
      value: v.meta && `${v.meta.title}(${v.name})`,
      path: v.path,
    })
  })

  const list = ref(listData)
  const selectItem = (item) => {
    router.push(item.path)
  }

  return {
    list,
    selectItem,
  }
}
